package com.techsoul.module.order.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.module.order.pojo.param.CartItem;
import com.techsoul.module.order.pojo.param.CartItemList;
import com.techsoul.module.order.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 购物车 控制器
 *
 * @author liuen
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/order/cart")
public class CartController {

    @Autowired
    private Result result;

    @Autowired
    private ICartService cartService;

    @ApiOperation("添加商品到购物车")
    @PostMapping("/addGoods")
    public Result addGoods(@Valid @RequestBody CartItem cartItem) {
        cartService.addGoods(cartItem);
        return result.success();
    }

    @ApiOperation("添加商品集合到购物车")
    @PostMapping("/addGoodsList")
    public Result addGoodsList(@RequestBody CartItemList cartItemList) {
        cartService.addGoodsList(cartItemList.getCartItems());
        return result.success();
    }

    @ApiOperation("删除购物车商品")
    @DeleteMapping("/delGoods/{skuId}")
    public Result delGoods(String type, @PathVariable("skuId") String skuId) {
        cartService.delGoods(type, skuId);
        return result.success();
    }

    @ApiOperation("删除选中的购物车商品")
    @DeleteMapping("/delCheckedGoods")
    public Result delCheckedGoods(String type) {
        cartService.delCheckedGoods(type);
        return result.success();
    }

    @ApiOperation("编辑购物车商品")
    @PutMapping("/editGoods")
    public Result editGoods(@Valid @RequestBody CartItem cartItem) {
        cartService.editGoods(cartItem);
        return result.success();
    }

    @ApiOperation("是否全选购物车商品")
    @PutMapping("/isCheckedAll")
    public Result isCheckedAll(@Valid @RequestBody CartItem cartItem) {
        cartService.isCheckedAll(cartItem);
        return result.success();
    }

    @ApiOperation("购物车商品列表")
    @GetMapping("/goodsList")
    public Result goodsList(String type) {
        return result.success(cartService.goodsList(type));
    }

    @ApiOperation("购物车中选中的商品列表")
    @PostMapping("/checkedGoodsList")
    public Result checkedGoodsList(String type) {
        return result.success(cartService.checkedGoodsList(type));
    }

}
